import goodsCapacityApi from "@/api/store/goodsCapacity";
import dictApi from "@/api/system/dict";
import { remove } from "nprogress";

export default {
  name: "goodscapacity",
  data() {
    return {
      formVisible: false,
      formTitle: "添加入库规格",
      rules: {
        goodsName: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        capacity: [
          { required: true, message: "请输入商品容量", trigger: "blur" },
        ],
        goodsType: [
          { required: true, message: "请选择商品类型", trigger: "change" },
        ],
        price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        priceUnit: [{ required: true, message: "请输入计量单位", trigger: "change" }],
      },
      listQuery: {
        current: 1,
        size: 20,
        criteria: {
          id: "",
          goodsName: "",
        goodsType: "",
        capacity: "",
        status: "0",
        type:""
        },
      },
      list: null,
      total: 0,
      listLoading: true,
      selRow: {},
      form: {
        id: "",
        goodsName: "",
        goodsType: "",
        specification: "",
        capacity: "",
        status: "0",
        num:1,
        type:"",
        price:1,
        priceUnit:""
      },
      formUpdate:{
        id:'',
        capacity:0,
        num:1
      },
      formUpdateVisible:false,
      goodsTypeList: [],
      inputVisible: false,
      inputValue: ""
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.fetchData();
      this.getTypeDict();
    },
    fetchData() {
      this.listLoading = true;
      goodsCapacityApi
        .getList(this.listQuery)
        .then((response) => {
          this.list = response.data.records;
          this.listLoading = false;
          this.total = response.data.total;
        })
        .catch(() => {});
    },
    getTypeDict() {
                  // 从后台获取字典列表
                  let dictName = "商品类型";
                  dictApi.getDicts(dictName).then((response) => {
                    this.goodsTypeList = response.data[0].children ?? [];
                  });
                },
    search() {
      this.listQuery.current = 1;
      this.fetchData();
    },
    reset() {
      this.listQuery.criteria.goodsName = "";
      this.listQuery.current = 1;
      this.fetchData();
    },
    handleFilter() {
      this.listQuery.current = 1;
      this.fetchData();
    },
    handleClose() {},
    fetchNext() {
      this.listQuery.current = this.listQuery.current + 1;
      this.fetchData();
    },
    fetchPrev() {
      this.listQuery.current = this.listQuery.current - 1;
      this.fetchData();
    },
    fetchPage(page) {
      this.listQuery.current = page;
      this.fetchData();
    },
    changeSize(limit) {
      this.listQuery.size = limit;
      this.fetchData();
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow;
    },

    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow;
    },
    resetForm() {
      this.form = {
        id: "",
        goodsName: "",
        goodsType: "",
        specification: [],
        capacity: 1,
        status: "0",
        type:"",
        price:1,
        priceUnit:""
      };
    },
    handleClose(tag) {
        this.form.specification.splice(this.form.specification.indexOf(tag), 1);
      },
      showInput() {
        this.inputVisible = true;
        this.$nextTick((_) => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },
      handleInputConfirm() {
        let from = this.form.specification;
        let inputValue = this.inputValue;
        if (inputValue) {
          from.push(inputValue);
        }
        this.inputVisible = false;
        this.inputValue = "";
      },
    add() {
        this.resetForm();
        this.formTitle = "添加入库规格";
        this.form.num = this.total +1
        this.formVisible = true;
      },
      save(){
        let self = this;
              this.$refs["form"].validate((valid) => {
                if (valid) {
                  goodsCapacityApi.save(this.form).then((response) => {
                    this.$message({
                      message: "提交成功",
                      type: "success",
                    });
                    self.resetForm();
                    self.fetchData();
                    self.formVisible = false;
                  });
                } else {
                  return false;
                }
              });
      },
      editItem(row){
        this.formUpdate.id = row.id
        this.formUpdate.capacity = row.capacity
        this.formUpdate.num = row.num
        this.formUpdateVisible = true
      },
      update(){
        let self = this
        if(this.formUpdate.id){
          goodsCapacityApi.update(this.formUpdate).then((response) => {
            this.$message({
              message: "提交成功",
              type: "success",
            });
            self.formUpdate.id = null
            self.formUpdate.capacity =0
            self.formUpdate.num = 1
                    self.fetchData();
                    self.formUpdateVisible = false;
          });
        }
      },
      removeItem(id){
        let self = this
        goodsCapacityApi.remove(id).then((response) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          self.fetchData();
        });
      },
      getTypeValueDict(num) {
        const arr = this.goodsTypeList;
        if (arr) {
          for (let i = 0; i < arr.length; i++) {
            if (arr[i].num == num) {
              return arr[i].name;
            }
          }
        }
      },
  },
}
